<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">交易编码</label>
        <el-input v-model="query.transcode" clearable placeholder="交易编码" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">交易说明</label>
        <el-input v-model="query.transdesc" clearable placeholder="交易说明" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">功能</label>
        <el-input v-model="query.funcid" clearable placeholder="功能" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">应用</label>
        <el-input v-model="query.appid" clearable placeholder="应用" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="交易编码" prop="transcode">
            <el-input v-model="form.transcode" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="交易说明" prop="transdesc">
            <el-input v-model="form.transdesc" style="width: 370px;" />
          </el-form-item>

          <el-form-item label="程序代码" prop="programcode">
            <el-input v-model="form.programcode" :rows="3" type="textarea" style="width: 370px;" />
          </el-form-item>

          <el-form-item label="应用系统">
            <el-select
              v-model="form.appid"
              filterable
              remote
              reserve-keyword
              :remote-method="remoteMethod"
              :loading="loading"
              @change="change"
            >
              <el-option
                v-for="item in appoptions"
                :key="item.value"
                :label="item.appname"
                :value="item.appid"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="功能">
            <el-cascader
              v-model="form.funcid"
              placeholder="功能选择"
              :disabled="disabledInput"
              :options="options"
              :props="{ checkStrictly: true ,emitPath:false}"
              filterable
              @focus="focus"
            />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="transcode" label="交易编码" />
        <el-table-column prop="transdesc" label="交易说明" />
        <el-table-column prop="funcname" label="功能" />
        <el-table-column prop="appname" label="应用" />
        <el-table-column prop="moduser" label="修改人" />
        <el-table-column prop="modtime" label="修改时间" />
        <el-table-column v-if="checkPer(['admin','baseSysTransaction:edit','baseSysTransaction:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudBaseSysTransaction from '@/api/appinfo/baseSysTransinfo'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { getByNames } from '@/api/appinfo/baseSysAppinfo'
import { getTrees } from '@/api/appinfo/baseSysFuncinfo'

const defaultForm = { content: null, transid: null, transcode: null, transdesc: null, transtype: null, programcode: null, funcid: null, appid: null, flag: null, notes: null, reguser: null, regtime: null, moduser: null, modtime: null }
export default {
  name: 'BaseSysTransaction',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '应用:交易信息', url: 'api/appinfo/baseSysTransaction', idField: 'transid', sort: 'transid,desc', crudMethod: { ...crudBaseSysTransaction }})
  },
  data() {
    return {
      appoptions: [],
      options: [],
      loading: false,
      disabledInput: true,
      editorOption: {},
      permission: {
        add: ['admin', 'baseSysTransaction:add'],
        edit: ['admin', 'baseSysTransaction:edit'],
        del: ['admin', 'baseSysTransaction:del']
      },
      rules: {
        transcode: [
          { required: true, message: '交易编码不能为空', trigger: 'blur' }
        ],
        transdesc: [
          { required: true, message: '交易说明不能为空', trigger: 'blur' }
        ],
        programcode: [
          { required: true, message: '程序代码不能为空', trigger: 'blur' }
        ],
        funcid: [
          { required: true, message: '功能不能为空', trigger: 'blur' }
        ],
        appid: [
          { required: true, message: '应用不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'transcode', display_name: '交易编码' },
        { key: 'transdesc', display_name: '交易说明' },
        { key: 'funcid', display_name: '功能' },
        { key: 'appid', display_name: '应用' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.afterToCU](crud, form) {
      this.appoptions = []
      // 初始化应用系统
      this.remoteMethod(form.appname)
      //   form.appid_copy = form.appid;
      //   form.appid = form.appname;

      if (this.form.appid) {
        this.disabledInput = false
        this.loading = false
        getTrees(this.form.appid).then((res) => {
          this.loading = false
          this.options = res
          console.log(res)
        }).catch(() => {
          this.loading = false
        })
      } else {
        this.disabledInput = true
      }
    },
    remoteMethod(query) {
      console.log('query:' + query)
      if (query !== '') {
        this.loading = true
        setTimeout(() => {
          this.loading = false
          getByNames(query).then((res) => {
            this.loading = false
            this.appoptions = res.filter(item => {
              return item.appname.toLowerCase()
                .indexOf(query.toLowerCase()) > -1
            })
            console.log(res)
          }).catch(() => {
            this.loading = false
          })
        }, 200)
      } else {
        this.appoptions = []
      }
    },
    focus() {
      if (!this.form.appid) {
        this.$message.error('请先选择应用系统!')
        this.disabledInput = true
      } else {
        this.disabledInput = false
      }
    },
    change() {
      if (this.form.appid) {
        this.loading = false
        getTrees(this.form.appid).then((res) => {
          console.log(this.form.appid)
          this.loading = false
          this.options = res
          console.log(res)
        }).catch(() => {
          this.loading = false
        })
        this.disabledInput = false
      } else {
        this.disabledInput = true
      }
    }
  }
}
</script>

<style scoped>

</style>
